package com.wechatapp.mengyao.poetry.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wechatapp.mengyao.poetry.core.PageResult;
import com.wechatapp.mengyao.poetry.core.TencentCosUtil;
import com.wechatapp.mengyao.poetry.entity.Media;
import com.wechatapp.mengyao.poetry.mapper.MediaMapper;
import com.wechatapp.mengyao.poetry.service.MediaService;
import com.wechatapp.mengyao.poetry.vo.MediaView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yanghuaxu
 * @date 2025/7/5 16:36
 */
@Service
public class MediaServiceImpl implements MediaService {

    @Autowired
    private MediaMapper mediaMapper;
    @Autowired
    private TencentCosUtil tencentCosUtil;

    @Override
    public PageResult<MediaView> getMediaListByAlbumId(Map<String, String> map) {
        int pageNum = Integer.parseInt(map.getOrDefault("pageNum", "1"));
        int pageSize = Integer.parseInt(map.getOrDefault("pageSize", "10"));
        Assert.isTrue(map.containsKey("albumId"), "缺少必填参数albumId");
        PageHelper.startPage(pageNum, pageSize, true);
        List<Media> mediaList = mediaMapper.getMediaListByAlbumId(map);
        List<MediaView> result = mediaList.stream().map(this::convert).collect(Collectors.toList());
        return PageResult.fromPage((Page<Media>) mediaList, result);
    }

    @Override
    public PageResult<MediaView> getMediaPageById(Map<String, String> map) {
        Assert.isTrue(map.containsKey("mediaId"), "缺少必填参数mediaId");
        Media media = mediaMapper.selectByPrimaryKey(Long.parseLong(map.get("mediaId")));
        int pageSize = Integer.parseInt(map.getOrDefault("pageSize", "10"));
        Assert.notNull(media, "媒体文件不存在");
        int position = mediaMapper.getMediaPosition(media.getAlbumId(), media.getId(), media.getSortOrder());
        int pageNum = (int) (position / pageSize) + 1;  // 目标页码
        map.put("pageNum", String.valueOf(pageNum));
        map.put("albumId", String.valueOf(media.getAlbumId()));
        return getMediaListByAlbumId(map);
    }

    @Override
    public MediaView getById(Long id) {
        Media media = mediaMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(media)) {
            return null;
        }
        return convert(media);
    }

    private MediaView convert(Media media) {
        TencentCosUtil.PreSigned preSigned = tencentCosUtil.generatePreSignedUrl(media.getFilePath(), new HashMap<>(), new HashMap<>());
        return new MediaView(media.getId(), media.getAlbumId(),
                media.getTitle(), media.getEpisodeSeq(), media.getSortOrder(),
                preSigned.getUrl(), preSigned.getExpirationDate(), media.getDuration(), media.getMediaType(),
                media.getFileSize(), media.getFileFormat());
    }
}
